package com.csf.service.impl;

import com.csf.dao.MessageMapper;
import com.csf.entity.Message;
import com.csf.service.MessageService;
import com.csf.util.MGWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MGWordFilter mgWordFilter;

    @Override
    public List<Message> findConversations(int userId, int initRows, int limit) {
        return messageMapper.findConversations(userId, initRows, limit);
    }

    @Override
    public int conversationCount(int userId) {
        return messageMapper.conversationCount(userId);
    }

    @Override
    public List<Message> findUserLetters(String conversationId, int initRows, int limit) {
        return messageMapper.findUserLetters(conversationId, initRows, limit);
    }

    @Override
    public int userletterCount(String conversationId) {
        return messageMapper.userletterCount(conversationId);
    }

    @Override
    public int userLetterUnRead(int userId, String conversationId) {
        return messageMapper.userLetterUnRead(userId, conversationId);
    }

    @Override
    public int insertMessage(Message message) {
//        敏感词过滤
        String content = message.getContent();
        message.setContent(HtmlUtils.htmlEscape(content));
        message.setContent(mgWordFilter.filter(message.getContent()));

        return messageMapper.insertMessage(message);
    }

    @Override
    public int updateLetterStatus(List<Integer> unreadLetter, int i) {
        return messageMapper.updateLetterStatus(unreadLetter, i);
    }

    @Override
    public int updateStatus(int i, int status) {
        return messageMapper.updateStatus(i,status);
    }

    @Override
    public Message findLastMessage(int userId, String topic) {
        return messageMapper.findLastMessage(userId, topic);
    }

    @Override
    public int findUnReadCount(int userId, String topic) {
        return messageMapper.findUnReadCount(userId, topic);
    }

    @Override
    public int findMessageTotal(int userId, String topic) {
        return messageMapper.findMessageTotal(userId, topic);
    }

    @Override
    public List<Message> findMessageList(int userId, String topic, int initRows, int limit) {
        return messageMapper.findMessageList(userId, topic, initRows, limit);
    }
}
